<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="飞机编号" prop="plane_id">
                <el-input v-model="form.plane_id" :disabled="disable"></el-input>
            </el-form-item>
            <el-form-item label="飞机类型" prop="plane_type">
                <el-select v-model="form.plane_type" placeholder="请选择飞机类型" :disabled="disable">
                    <el-option label="小型机" value="0"></el-option>
                    <el-option label="中型机" value="1"></el-option>
                    <el-option label="大型机" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="有效日期" prop="plane_buyDate">
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择购买日期" v-model="form.plane_buyDate" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="1" :offset="1">-</el-col>
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择有效日期" v-model="form.plane_endDate" style="width: 100%;"></el-date-picker>
                </el-col>
            </el-form-item>
            <el-form-item label="经济舱" prop="touristClassNum">
                <el-input v-model="form.touristClassNum" :disabled="disable"></el-input>
            </el-form-item>
            <el-form-item label="商务舱" prop="businessClassNum">
                <el-input v-model="form.businessClassNum" :disabled="disable"></el-input>
            </el-form-item>
            <el-form-item label="头等舱" prop="firstClassNum">
                <el-input v-model="form.firstClassNum" :disabled="disable"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addPlane" v-if="!disable">立即创建</el-button>
                <el-button type="primary" @click="savePlane" v-else>保存信息</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name:'plane-editor',
    props:{
        form:{
            type:Object
        },
        disable:{
            type:Boolean
        }
    },
    data(){
        return{
            rules:{
                plane_id:[
                    { required: true, message: '请输入飞机编号', trigger: 'blur' }
                ],
                plane_type:[
                    { required: true, message: '请输入飞机类型', trigger: 'blur' }
                ],
                touristClassNum:[
                    { required: true, message: '请输入经济舱数量', trigger: 'blur' }
                ],
                businessClassNum:[
                    { required: true, message: '请输入商务舱数量', trigger: 'blur' }
                ],
                firstClassNum:[
                    { required: true, message: '请输入头等舱数量', trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
        addPlane(){
            let item = this.form
            if (this.checkEmpty(item.plane_id, "飞机编号") || this.checkEmpty(item.plane_type, "飞机类型") ||
                this.checkEmpty(item.plane_buyDate, "购买日期") || this.checkEmpty(item.plane_endDate, "有效日期") || 
                this.checkEmpty(item.touristClassNum, "经济舱数量") || this.checkEmpty(item.businessClassNum, "商务舱数量")||
                this.checkEmpty(item.firstClassNum, "头等舱数量")) {
                return;
            }
            console.log(item)
            item.admin_id = this.$cookies.get("admin").admin_id;
            this.$http.post("/api/plane/addPlane",item).then((res)=>{
                console.log(res);
                this.showMessage("添加成功","success");
                this.$emit('closeDialog');
                this.$emit('refresh');
            })
        },
        savePlane(){
            let item = this.form
            if (this.checkEmpty(item.plane_id, "飞机编号") || this.checkEmpty(item.plane_type, "飞机类型") ||
                this.checkEmpty(item.plane_buyDate, "购买日期") || this.checkEmpty(item.plane_endDate, "有效日期") || 
                this.checkEmpty(item.touristClassNum, "经济舱数量") || this.checkEmpty(item.businessClassNum, "商务舱数量")||
                this.checkEmpty(item.firstClassNum, "头等舱数量")) {
                return;
            }
            console.log(item)
            item.admin_id = this.$cookies.get("admin").admin_id;
            this.$http.post("/api/plane/editPlane",item).then((res)=>{
                console.log(res);
                this.showMessage("修改成功","success");
                this.$emit('closeDialog');
            })
        }
    }
}
</script>